﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service;
using Quartz;
using SMECustomerWebsite.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SMECustomerWebsite.Jobs
{
    public class ReturnResultCreateDaoRequest : JobBase, IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            List<RequestCreateDaoRecord> listRequestNeedSendMail = RetrieveListCreateDaoRequest();
            if (listRequestNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO thanh cong";
                listRequestNeedSendMail.ForEach(x =>
                {
                    List<string> listCC = new List<string>();
                    if (!string.IsNullOrEmpty(x.Pic))
                    {
                        listCC.Add(x.Pic + "@vpbank.com.vn");
                    }
                    EmailContent content = new EmailContent
                    {
                        Subject = mailSubject,
                        Body = string.Format("Yeu cau khoi tao DAO cua {0} da thanh cong, ma DAO cua ban hien tai la: {1} ", x.FullName, x.Dao),
                        CC = listCC,
                        To = x.Email
                    };
                    if (SendEmail(content))
                    {
                        x.StatusCreateDaoRequest = (int)StatusRequestCreateDaoEnum.SendEmail;
                    }
                });
                var listNeedUpdateStatus = listRequestNeedSendMail.Where(x => x.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.SendEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
            List<RequestCreateDaoRecord> listRequestRejectedNeedSendMail = RetrieveListCreateDaoRequestRejected();
            if (listRequestRejectedNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO bi tu choi";
                listRequestRejectedNeedSendMail.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.RejectReason))
                    {

                        List<string> listCC = new List<string>();
                        if (!string.IsNullOrEmpty(x.Pic))
                        {
                            listCC.Add(x.Pic + "@vpbank.com.vn");
                        }
                        EmailContent content = new EmailContent
                        {
                            Subject = mailSubject,
                            Body = string.Format("Yeu cau khoi tao DAO cua {0} da bi tu choi. Ly do la: {1}", x.FullName, x.RejectReason),
                            CC = listCC,
                            To = x.Email
                        };
                        if (SendEmail(content))
                        {
                            x.StatusCreateDaoRequest = (int)StatusRequestCreateDaoEnum.SendRejectEmail;
                        }
                    }
                });
                var listNeedUpdateStatus = listRequestRejectedNeedSendMail.Where(x => x.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.SendRejectEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
        }

        private List<RequestCreateDaoRecord> RetrieveListCreateDaoRequest()
        {
            RetrieveListRequestCreateDaoByUserResponse result = new RetrieveListRequestCreateDaoByUserResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusRequestCreateDaoEnum.Done
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListRequestCreateDaoByUserRequest request = new RetrieveListRequestCreateDaoByUserRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListRequestCreateDaoByUser(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<RequestCreateDaoRecord>() : result.Records;
            return listRecord;
        }
        private List<RequestCreateDaoRecord> RetrieveListCreateDaoRequestRejected()
        {
            RetrieveListRequestCreateDaoByUserResponse result = new RetrieveListRequestCreateDaoByUserResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusRequestCreateDaoEnum.Reject
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListRequestCreateDaoByUserRequest request = new RetrieveListRequestCreateDaoByUserRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListRequestCreateDaoByUser(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<RequestCreateDaoRecord>() : result.Records;
            return listRecord;
        }
    }
}